/**
 * 
 */
package designpatterns.mvc.calculator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Cris
 * 
 */
public class CalculatorController 
{
	final private CalculatorView theView;
	final private CalculatorModel theModel;

	public CalculatorController(CalculatorView theView, CalculatorModel theModel) 
	{
		this.theView = theView;
		this.theModel = theModel;

		this.theView.addCalculateListener(new CalculateListener());
	}

	class CalculateListener implements ActionListener 
	{

		public void actionPerformed(ActionEvent e) 
		{
			int firstNumber, secondNumber = 0;

			try {

				firstNumber = theView.getFirstNumber();
				secondNumber = theView.getSecondNumber();

				theModel.addTwoNumbers(firstNumber, secondNumber);

				theView.setCalcSolution(theModel.getCalculationValue());
			}

			catch (NumberFormatException ex) 
			{
				System.out.println(ex);
			}
		}
	}
}
